#ifndef _QI_GRAMMAR_H
#define _QI_GRAMMAR_H

#include "../q_common.h"

struct qi_grammarplaceholder;
enum qi_grammarplaceholdertype {GRAMMAR_LEAF = 0, GRAMMAR_NODE, GRAMMAR_ROOT};
enum qi_grammarnodetype {NODE_OR = 0, NODE_AND};

struct qi_grammarchild
{
	int min, max;
	struct qi_grammarplaceholder* ptr;

	struct qi_grammarchild* next;
};

struct qi_grammarleaf
{
	int (*validate)(const char* const str);
	int (*dominating_branch)(const char* const str);
	int (*cancel)();
};

struct qi_grammarnode
{
	enum qi_grammarnodetype type;

	int num_children;
	int current_children;
	struct qi_grammarchild* child;
};

struct qi_grammarplaceholder
{
	char name[Q_MAX_SYMBOL_LENGTH];

	enum qi_grammarplaceholdertype type;
	union
	{
		struct qi_grammarnode node;
		struct qi_grammarleaf leaf;
	};
	struct qi_grammarplaceholder* next;
};

void qi_initgrammar();

struct qi_grammarplaceholder* qi_grammarplaceholderlookup(const char* const name);
int qi_addgrammarleaf(const char* const name, int (*validate)(const char* const str), int (*dominating_branch)(const char* const str));
int qi_addgrammarnode(const char* const name, enum qi_grammarnodetype type);
int qi_addtogrammarnode(const char* const parent, const char* const child, int min, int max);

extern struct qi_grammarplaceholder* qi_grammar[Q_HASH_TABLE_SIZE];

#endif

